//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.zaicheng.cloud.remotecall.template;

import com.zaicheng.cloud.remotecall.Util;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class Expressions {
    private static Map<Pattern, Class<? extends Expression>> expressions = new LinkedHashMap();

    public Expressions() {
    }

    public static Expression create(String value) {
        String expression = stripBraces(value);
        if (expression != null && !expression.isEmpty()) {
            Optional<Entry<Pattern, Class<? extends Expression>>> matchedExpressionEntry = expressions.entrySet().stream().filter((entry) -> {
                return ((Pattern)entry.getKey()).matcher(expression).matches();
            }).findFirst();
            if (!matchedExpressionEntry.isPresent()) {
                return null;
            } else {
                Entry<Pattern, Class<? extends Expression>> matchedExpression = (Entry)matchedExpressionEntry.get();
                Pattern expressionPattern = (Pattern)matchedExpression.getKey();
                String variableName = null;
                String variablePattern = null;
                Matcher matcher = expressionPattern.matcher(expression);
                if (matcher.matches()) {
                    variableName = matcher.group(2).trim();
                    if (variableName.contains(":")) {
                        String[] parts = variableName.split(":");
                        variableName = parts[0];
                        variablePattern = parts[1];
                    }

                    if (variableName.contains("{")) {
                        return null;
                    }
                }

                return new Expressions.SimpleExpression(variableName, variablePattern);
            }
        } else {
            throw new IllegalArgumentException("an expression is required.");
        }
    }

    private static String stripBraces(String expression) {
        if (expression == null) {
            return null;
        } else {
            return expression.startsWith("{") && expression.endsWith("}") ? expression.substring(1, expression.length() - 1) : expression;
        }
    }

    static {
        expressions.put(Pattern.compile("^([+#./;?&]?)(.*)$"), Expressions.SimpleExpression.class);
    }

    static class SimpleExpression extends Expression {
        SimpleExpression(String expression, String pattern) {
            super(expression, pattern);
        }

        String encode(Object value) {
            return UriUtils.encode(value.toString(), Util.UTF_8);
        }

        @Override
        String expand(Object variable, boolean encode) {
            StringBuilder expanded = new StringBuilder();
            if (Iterable.class.isAssignableFrom(variable.getClass())) {
                expanded.append(this.expandIterable((Iterable)variable));
            } else {
                expanded.append(encode ? this.encode(variable) : variable);
            }

            String result = expanded.toString();
            if (!this.matches(result)) {
                throw new IllegalArgumentException("Value " + expanded + " does not match the expression pattern: " + this.getPattern());
            } else {
                return result;
            }
        }

        private String expandIterable(Iterable<?> values) {
            StringBuilder result = new StringBuilder();
            Iterator var3 = values.iterator();

            while(var3.hasNext()) {
                Object value = var3.next();
                if (value != null) {
                    String expanded = this.encode(value);
                    if (expanded.isEmpty()) {
                        result.append(",");
                    } else {
                        if (result.length() != 0 && !result.toString().equalsIgnoreCase(",")) {
                            result.append(",");
                        }

                        result.append(expanded);
                    }
                }
            }

            return result.toString();
        }
    }
}
